<template>
  <div class="box">
    <header class="kindHeader header">
      <van-nav-bar title="分类"
                   left-text="返回"
                    @click-left="$router.back()"
                   left-arrow>

        <template #right>

          <van-icon name="search"
                    size="18" />

        </template>
      </van-nav-bar>
    </header>
    <div class="content kindCon">
      <van-sidebar v-model="activeKey"
                   @change="onChange">
        <van-sidebar-item :title="item"
                          v-for="item in columnList"
                          :key="item" />

      </van-sidebar>
      <van-grid :column-num="4">
        <van-grid-item :icon="item.logo"
                       :text="item.brand"
                       v-for="item in brandList"
                       :key="item.brand"
                       @click="searchbrand(item.brand)"
                        />
      </van-grid>

    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Icon, Sidebar, SidebarItem, Grid, GridItem } from 'vant'
import { getColumn, getColumnBrand } from '../../api/index.js'
Vue.use(NavBar)
Vue.use(Icon)
Vue.use(Sidebar)
Vue.use(SidebarItem)

Vue.use(Grid)
Vue.use(GridItem)

export default {
  data () {
    return {
      activeKey: 0,
      columnList: [],
      brandList: []
    }
  },
  methods: {
    // index为组件自带索引
    onChange (index) {
      this.activeKey = index
      console.log(this.activeKey)
      getColumnBrand({
        column: this.columnList[index]
      }).then(res => {
        this.brandList = res.data.data
        console.log(this.brandList)
      })
    },
    searchbrand (brand) {
      const a = this.columnList[this.activeKey]
      this.$router.push('/brand?column=' + a +'&brand=' + brand )
    }

  },
  mounted () {
    getColumn().then(res => {
      // console.log(res)
      this.columnList = res.data.data
      console.log(this.columnList)
      this.onChange(0)
    })
  }
}
</script>

<style >
.kindCon {
  display: flex;
}

.van-sidebar {
  width: 1.2rem;
}

.van-grid {
  align-self: flex-start;
  width: 100%;
}
</style>